package com.xjrsoft.module.customerTwo.AppManage.daykpi.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customerTwo.AppManage.daykpi.dto.DwDayKpiDataDto;
import com.xjrsoft.module.customerTwo.AppManage.daykpi.dto.SaveDwDayKpiDataFormDataDto;
import com.xjrsoft.module.customerTwo.AppManage.daykpi.vo.DwDayKpiDataVo;
import com.xjrsoft.module.customerTwo.AppManage.daykpi.vo.DwDayKpiDataListVo;
import com.xjrsoft.module.customerTwo.AppManage.daykpi.dto.DwDayKpiDataListDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.IoUtil;
import com.xjrsoft.module.excel.entity.XjrExcelImport;
import com.xjrsoft.module.excel.factory.ParserFactory;
import com.xjrsoft.module.excel.parser.ImportDataParser;
import com.xjrsoft.module.excel.service.IXjrExcelImportService;
import org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.time.LocalDateTime;
import com.xjrsoft.common.serializers.LocalDateTimeDeserializer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.AppManage.daykpi.entity.DwDayKpiData;
import com.xjrsoft.module.customerTwo.AppManage.daykpi.vo.DwDayKpiDataVo;
import com.xjrsoft.module.customerTwo.AppManage.daykpi.service.IDwDayKpiDataService;

/**
 *  控制器
 *
 * @author lisen
 * @since 2024-05-31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/daykpi")
@Api(value = "日指标", tags = "日指标接口")
public class DwDayKpiDataController {

	private final IXjrExcelImportService importService;


	private final IDwDayKpiDataService dwDayKpiDataService;


	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入dwDayKpiData")
	@SaCheckPermission(value = {"daykpi:view", "daykpi:modify"}, mode = SaMode.OR)
	public Response<DwDayKpiDataVo> getFormData(@PathVariable String id) {
		// 主表数据
		DwDayKpiData dwDayKpiData = dwDayKpiDataService.getById(id);
		DwDayKpiDataVo dwDayKpiDataVo = BeanUtil.copy(dwDayKpiData, DwDayKpiDataVo.class);
		return Response.ok(dwDayKpiDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入dwDayKpiData")
		public Response<PageOutput<DwDayKpiDataListVo>> getPageList(DwDayKpiDataListDto listDto) {
		IPage<DwDayKpiData> page = dwDayKpiDataService.getPageList(listDto);
		List<DwDayKpiDataListVo> records = BeanUtil.copyList(page.getRecords(), DwDayKpiDataListVo.class);
			// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入dwDayKpiData")
	@SaCheckPermission("daykpi:add")
	public Response save(@RequestBody SaveDwDayKpiDataFormDataDto formDto) {
		DwDayKpiDataDto dwDayKpiDataDto = formDto.getDwDayKpiDataDto();
		DwDayKpiData dwDayKpiData = BeanUtil.copy(dwDayKpiDataDto, DwDayKpiData.class);
		Integer dataId = dwDayKpiDataService.addDwDayKpiData(dwDayKpiData);
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{iid}")
	@ApiOperation(value = "修改", notes = "传入dwDayKpiData")
	@SaCheckPermission("daykpi:modify")
	public Response update(@PathVariable String iid, @RequestBody SaveDwDayKpiDataFormDataDto formDto) {
		Integer id = Integer.parseInt(iid);
		DwDayKpiDataDto dwDayKpiDataDto = formDto.getDwDayKpiDataDto();
		DwDayKpiData dwDayKpiData = BeanUtil.copy(dwDayKpiDataDto, DwDayKpiData.class);
		return Response.status(dwDayKpiDataService.updateDwDayKpiData(id, dwDayKpiData));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"daykpi:batchDelete", "daykpi:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(dwDayKpiDataService.delete(ids));
	}


	/**
	 * excel批量导入数据
	 */
	@PostMapping("/import")
	@ApiOperation(value = "excel批量导入数据", notes = "传入菜单id")
	@SaCheckPermission("daykpi:import")
	public Response importData(@RequestParam("F_ModuleId") String moduleId, MultipartFile file) {
		XjrExcelImport excelImport = importService.getByModuleId(moduleId);
		ImportDataParser parser = ParserFactory.getImportDataParser();
		File excelFile = IoUtil.toFile(file);
		List<Map<String, Object>> recordList = parser.parseDataToMap(excelImport, excelFile);
		if (CollectionUtil.isNotEmpty(recordList)) {
			List<DwDayKpiDataDto> entityDtoList = recordList.stream().map(record ->
				BeanUtil.toDtoBean(record, DwDayKpiDataDto.class)
			).collect(Collectors.toList());
			List<DwDayKpiData> entities = BeanUtil.copyList(entityDtoList, DwDayKpiData.class);
			return Response.status(dwDayKpiDataService.saveBatch(entities));
		}
		return Response.notOk();
	}
}
